package com.tanhua.server.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.autoconfig.template.HuanXinTemplate;
import com.tanhua.commons.utils.Constants;
import com.tanhua.dubbo.api.*;
import com.tanhua.model.domain.*;
import com.tanhua.model.domain.mongo.Comment;
import com.tanhua.model.domain.mongo.Friend;
import com.tanhua.model.domain.mysql.Announcement;
import com.tanhua.model.domain.mysql.User;
import com.tanhua.model.domain.mysql.UserInfo;
import com.tanhua.model.domain.vo.CommentVo;
import com.tanhua.model.domain.vo.ContactVo;
import com.tanhua.model.domain.vo.UserInfoVo;
import com.tanhua.server.exception.BusinessException;
import com.tanhua.server.exception.ErrorResult;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author lk
 * @date 2022-03-31
 */
@Service
public class MessagesService {

    @DubboReference
    private UserApi userApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    @DubboReference
    private FriendApi friendApi;

    @DubboReference
    private CommentApi commentApi;

    @DubboReference
    private AnnouncementApi announcementApi;

    /**
     * 根据环信id查询用户详情
     */
    public UserInfoVo findUserInfoByHuanxin(String huanxinId) {
        //根据环信id查询用户
        User user = userApi.findByHuanxin(huanxinId);
        //根据用户id查询用户详情
        UserInfo userInfo = userInfoApi.findById(user.getId());
        UserInfoVo vo = new UserInfoVo();
        //copy同名同类型的属性
        BeanUtils.copyProperties(userInfo, vo);
        //因为vo里面的age属性是String的,所以需要单独赋值
        if (userInfo.getAge() != null) {
            vo.setAge(userInfo.getAge().toString());
        }
        return vo;
    }

    //添加好友关系
    public void contacts(Long friendId) {
        Boolean aBoolean = huanXinTemplate.addContact(Constants.HX_USER_PREFIX + UserHolder.getUserId(), Constants.HX_USER_PREFIX + friendId);
        if (!aBoolean) {
            throw new BusinessException(ErrorResult.error());
        }
        //如果注册成功，记录好友关系到mongodb
        friendApi.save(friendId, UserHolder.getUserId());
    }

    //分页查询联系人列表
    public PageResult findFriends(Integer page, Integer pagesize, String keyword) {
        //查询出所有好友
        List<Friend> list = friendApi.findByUserId(UserHolder.getUserId(), page, pagesize);
        if (CollUtil.isEmpty(list)) {
            return new PageResult();
        }
        //获得所有好友的 id
        List<Long> friendIds = CollUtil.getFieldValues(list, "friendId", Long.class);
        //调用UserInfoAPI查询好友的用户详情
        UserInfo userInfo = new UserInfo();
        userInfo.setNickname(keyword); //添加昵称条件
        Map<Long, UserInfo> map = userInfoApi.findByIds(friendIds, userInfo);
        List<ContactVo> contactVoList = new ArrayList<>();
        for (Friend friend : list) {
            //获取 id 对应的详细信息
            UserInfo userInfo1 = map.get(friend.getFriendId());
            if (userInfo1 != null){
                ContactVo vo = ContactVo.init(userInfo1);
                contactVoList.add(vo);
            }
        }
        return new PageResult(page,pagesize,0,contactVoList);
    }

    //喜欢/点赞/评论  列表
    public PageResult findLoves(Integer page, Integer pagesize, Long userId, CommentType love) {
        //查询点赞列表
        List<Comment> list = commentApi.findCommentLove(page,pagesize,userId,love);
        if (CollUtil.isEmpty(list)){
            return new PageResult();
        }
        List<Long> userIds = CollUtil.getFieldValues(list, "userId", Long.class);
        //查询点赞人的详细信息
        Map<Long, UserInfo> map = userInfoApi.findByIds(userIds,null);
        List<CommentVo> vos = new ArrayList<>();
        for (Comment comment : list) {
            UserInfo userInfo = map.get(comment.getUserId());
            CommentVo init = CommentVo.init(userInfo, comment);
            vos.add(init);
        }
        return new PageResult(page,pagesize,0,vos);

    }

    //查询公告
    public PageResult findAnnouncements(Integer page, Integer pagesize) {
        Page<Announcement> announcementPage =announcementApi.findAll(page,pagesize);
        if (announcementPage==null){
            return new PageResult();
        }
        return new  PageResult(page,pagesize,0,announcementPage.getRecords());

    }
}
